//
//  NSAttributedString+ZBTension.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/3/24.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "NSAttributedString+ZBTension.h"
#import <CoreText/CoreText.h>

@implementation NSAttributedString (ZBTension)

- (CFIndex)getLineCountWithSize:(CGSize)size
{
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)self);
    
    CGMutablePathRef framePath = CGPathCreateMutable();
    CGPathAddRect(framePath, NULL, CGRectMake(0, 0, size.width, size.height));
    // Get the frame that will do the rendering.
    CFRange currentRange = CFRangeMake(0, 0);
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
    CGPathRelease(framePath);
    
    // Get each of the typeset lines
    NSArray *lines = (__bridge id)CTFrameGetLines(frameRef);
    
    CFIndex linesCount = [lines count];
    CFRelease(frameRef);
    CFRelease(framesetter);

    return linesCount+1;
}

@end
